package com.hmjk.health.views;

import com.hmjk.health.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.MeasureSpec;


public class HorizontalPillarView extends View{
	private int YELLOW = 0xffffb11a;
	private int GREEN = 0xff04ac10;
	private int RED = 0xffff0000;
	
	private float Min = 1.0f,Max = 33.0f, NormalMin,NormalMax,CurValue;
	private Bitmap cursorDrawable = null;
	private int isCurNormal = 0;   //-1 < ,0 ,1
	private float mtextSize = 0;
	private int mtextColor = 0xff444444;
	private int mpillarHeight = 0;
	private String topTip = null,bottomTip = null;
	
	public HorizontalPillarView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub

	}
	
	public HorizontalPillarView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		init(context,attrs);
	}

	public HorizontalPillarView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		init(context,attrs);
	}	
	
	private void init(Context context, AttributeSet attrs)
	{
		if(null != attrs){
			TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HorizontalPillar);
			int num = a.getIndexCount();
			for(int i=0; i<num; i++){
				int attr = a.getIndex(i);
				switch (attr) {
					case R.styleable.HorizontalPillar_textSize:
						mtextSize = a.getDimensionPixelSize(attr, (int) mtextSize);
					break;
					case R.styleable.HorizontalPillar_pillarHeight:
						mpillarHeight = a.getDimensionPixelSize(attr,mpillarHeight);
					break;
				}
			}
			
			a.recycle();
		}		
	}

    public void SetParameter(float min, float max, float nMin, float nMax, float cur ){
    	Min = min;
    	Max = max;
    	NormalMin = nMin;
    	NormalMax = nMax;
    	CurValue = cur;
    	
    	if(cursorDrawable != null)
    	{
    		cursorDrawable.recycle();
    	}   
    	
    	if(CurValue < NormalMin)
    	{
    		isCurNormal = -1;
    		cursorDrawable = BitmapFactory.decodeResource(getResources(), R.drawable.health_cursor_yellow);
    	}
    	else if(CurValue > NormalMax)
    	{
    		isCurNormal = 1;
    		cursorDrawable = BitmapFactory.decodeResource(getResources(), R.drawable.health_cursor_red);
    	}
    	else
    	{
    		isCurNormal = 0;
    		cursorDrawable = BitmapFactory.decodeResource(getResources(), R.drawable.health_cursor_green);
    	}  
    	invalidate();
    }
    
    public void setTipStr(String s1,String s2)
    {
    	this.topTip = s1;
    	this.bottomTip = s2;
    }
    
	@Override
	protected void onAttachedToWindow(){
		super.onAttachedToWindow();
    	if(cursorDrawable == null)
    	{
       		cursorDrawable = BitmapFactory.decodeResource(getResources(), R.drawable.health_cursor_green);
    	}
	}
	
	@Override
	protected void onDetachedFromWindow(){		
    	if(cursorDrawable != null)
    	{
    		cursorDrawable.recycle();
    		cursorDrawable = null;
    	}  
    	super.onDetachedFromWindow();
	}	    
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
		
		int widthMode = MeasureSpec.getMode(widthMeasureSpec);
		int widthSize = MeasureSpec.getSize(widthMeasureSpec);	
		int heightMode = MeasureSpec.getMode(heightMeasureSpec);
		int heightSize = MeasureSpec.getSize(heightMeasureSpec);
		


			heightSize = (int) (innerMargin + mtextSize + innerMargin + cursorDrawable.getHeight() + mpillarHeight + innerMargin + mtextSize +innerMargin); 

		
		//int size = (widthSize>heightSize)?widthSize:heightSize; 
		
		//int wSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY);
		int hSpec = MeasureSpec.makeMeasureSpec(heightSize,heightMode);		
		super.onMeasure(widthMeasureSpec, hSpec);
	}
	
	private final static int innerMargin = 2;
	
	@Override
	protected void onDraw(Canvas canvas){
		super.onDraw(canvas);	

    	
		Paint mPaint = new Paint();
		mPaint.setTextSize(mtextSize);
		mPaint.setStyle(Paint.Style.FILL);
		mPaint.setColor(mtextColor);
		mPaint.setStrokeJoin(Paint.Join.ROUND);   
		mPaint.setStrokeCap(Paint.Cap.ROUND);   
		mPaint.setStrokeWidth(3);   		
		mPaint.setAntiAlias(true);  
		
		float cursorh = (float)cursorDrawable.getHeight();
    	float w = getWidth();
    	float h = this.mpillarHeight;
    	float fontH = this.mtextSize;
    	float curW = (float)cursorDrawable.getWidth();
    	
    	w = w - h;
    	
		float curX = w * (CurValue-Min)/((Max-Min)) + h/2;		
		
		if(topTip != null)
		{
			Rect tmpR = new Rect();
			float strW = mPaint.measureText(topTip);
			float pos = curX - strW/2;
			if(pos < 0)
				pos = 0;
			
			if((curX + strW/2) > (getWidth()-1))
				pos = getWidth() - strW - 1;
			
			canvas.drawText(topTip, pos, fontH+innerMargin, mPaint);
		}
		if(curX > ((float)getWidth()-curW/2))
		{
			curX = ((float)getWidth()-curW/2);
		}
		canvas.drawBitmap(cursorDrawable, curX-(float)cursorDrawable.getWidth()/2, fontH+2*innerMargin, mPaint);   
		
		mPaint.setColor(YELLOW);
		RectF r1 = new RectF();
		r1.left = 0;
		r1.top = cursorh + fontH+2*innerMargin;
		r1.right = h;
		r1.bottom = r1.top+mpillarHeight;
		
		canvas.drawArc(r1, 90, 180, true, mPaint);
		
		r1.left = h/2;
		r1.right = r1.left + w * (NormalMin-Min)/(Max-Min);
		canvas.drawRect(r1, mPaint);
		
		r1.left = r1.right;
		r1.right =r1.left + w * (NormalMax - NormalMin)/(Max-Min);		
		mPaint.setColor(GREEN);
		canvas.drawRect(r1, mPaint);
		
		r1.left = r1.right;
		r1.right = w+h/2;		
		mPaint.setColor(RED);
		canvas.drawRect(r1, mPaint);	
		
		r1.left = w-1;
		r1.right = w+h-1;		
		canvas.drawArc(r1, 270, 180, true, mPaint);

		mPaint.setColor(mtextColor);
		if(bottomTip != null)
		{
			Rect tmpR = new Rect();
			float strW = mPaint.measureText(bottomTip);
			float pos =  curX - strW/2;
			if(pos < 0)
				pos = 0;
			
			if((curX + strW/2) > (getWidth()-1))
				pos = getWidth() - strW - 1;	
			
			canvas.drawText(bottomTip, pos, fontH+2*innerMargin+cursorh+h+innerMargin+fontH, mPaint);
		}		
	}
}